package com.spogher.bsc.pages;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.BscSession;
import com.spogher.bsc.data.dao.interfaces.PlayerDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Player;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.components.dropdownChoices.PlayerPositionDropDownChoice;
import com.spogher.components.dropdownChoices.TeamDropDownChoice;
import com.spogher.components.textfields.DatePickerTextField;

public class PlayerPage extends BscTemplatePage{
	
	@SpringBean
	private PlayerDao playerDao;
	@SpringBean
	private TeamDao teamDao;

    public	PlayerPage() {
    	super();
		Form<Player> playerForm = new Form<Player>("playerForm", new CompoundPropertyModel<Player>(new Player()));
		playerForm.add(new TextField<String>("name").setRequired(true));
		playerForm.add(new TextField<String>("surname").setRequired(true));
		playerForm.add(new TextField<String>("height").setRequired(true));
		playerForm.add(new TextField<String>("weight").setRequired(true));
		playerForm.add(new DatePickerTextField("birthDate").setRequired(true));
		List<Team> teamList;
		if (BscSession.get().isRootUser()) {
			teamList = teamDao.findAll();
		}
		else {
			teamList = teamDao.findByManager(BscSession.get().getManager());
		}
		DropDownChoice<Team> teamChoice = new TeamDropDownChoice("team", teamList);	
		playerForm.add(teamChoice);
		DropDownChoice<Player.Position> positionChoice = new PlayerPositionDropDownChoice("position");
		playerForm.add(positionChoice);
		
		final WebMarkupContainer wmc = new WebMarkupContainer("listContainer");
		
		wmc.add(new ListView<Player>("list", new PropertyModel<List<Player>>(this, "playerDao.findAll")){

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Player> item) {
				Player player = item.getModelObject();
				item.add(new Label("playerName", player.getName()));
				item.add(new Label("playerTeam", player.getTeam().getName()));			
			}
			
		});
		wmc.setOutputMarkupId(true);
		add(wmc);
		
		playerForm.add(new AjaxSubmitLink("submit"){
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Player player = (Player) form.getModelObject();
				Player newPlayer = new Player();
				newPlayer.setName(player.getName());
				newPlayer.setSurname(player.getSurname());
				newPlayer.setTeam(player.getTeam());
				newPlayer.setPosition(player.getPosition());
				newPlayer.setWeight(player.getWeight());
				newPlayer.setHeight(player.getHeight());
				newPlayer.setBirthDate(player.getBirthDate());
				playerDao.save(newPlayer);
				target.addComponent(wmc);
			}
		});
		
		
		add(playerForm);
    }
    
}
